package org.darcy.controller.admin.site;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.darcy.entity.BizAd;
import org.darcy.framework.blog.business.enums.ResponseStatus;
import org.darcy.framework.model.PageResult;
import org.darcy.framework.model.ResponseVO;
import org.darcy.framework.util.ResultUtil;
import org.darcy.model.BizAdModel;
import org.darcy.service.BizAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.util.StrUtil;

/**
 * 广告管理
 */
@RestController
@RequestMapping("/bizAd")
public class BizAdController {

	@Autowired
	private BizAdService bizAdService;

	@RequiresPermissions("bizAds")
	@PostMapping("/list")
	public PageResult list(BizAdModel param) {
		Page p = new Page(param.getP(), param.getPageSize());
		QueryWrapper<BizAd> q = new QueryWrapper<BizAd>();
		if (StrUtil.isNotEmpty(param.getTitle()))
			q.like("title", param.getTitle());
		IPage pageInfo = bizAdService.getPage(p, q);
		return ResultUtil.tablePage(pageInfo);
	}

	@RequiresPermissions("bizAd:add")
	@PostMapping(value = "/add")
	public ResponseVO add(BizAd bizAd) {
		BizAd entity = bizAdService.getByPosition(bizAd.getPosition());
		if (null != entity) {
			return ResultUtil.error("当前广告位已存在广告，一个广告位仅支持一条广告");
		}
		bizAdService.insert(bizAd);
		return ResultUtil.success("成功");
	}

	@RequiresPermissions(value = { "bizAd:batchDelete", "bizAd:delete" }, logical = Logical.OR)
	@PostMapping(value = "/remove")
	public ResponseVO remove(Long[] ids) {
		if (null == ids) {
			return ResultUtil.error(500, "请至少选择一条记录");
		}
		for (Long id : ids) {
			bizAdService.delete(id);
		}
		return ResultUtil.success("成功删除 [" + ids.length + "] 条记录");
	}

	@RequiresPermissions("bizAd:edit")
	@PostMapping("/get/{id}")
	public ResponseVO get(@PathVariable Long id) {
		return ResultUtil.success(null, this.bizAdService.getById(id));
	}

	@RequiresPermissions("bizAd:edit")
	@PostMapping("/edit")
	public ResponseVO edit(BizAd bizAd) {
		try {

			BizAd entity = bizAdService.getById(bizAd.getId());
			if (null == entity) {
				return ResultUtil.error("广告不存在！");
			}
			if (!entity.getPosition().equals(bizAd.getPosition())) {
				entity = bizAdService.getByPosition(bizAd.getPosition());
				if (null != entity) {
					return ResultUtil.error("当前广告位已存在广告，一个广告位仅支持一条广告");
				}
			}

			bizAdService.update(bizAd);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("数据修改失败！");
		}
		return ResultUtil.success(ResponseStatus.SUCCESS);
	}

}
